package com.hl.sfzy.controller.consult;


import com.github.pagehelper.PageInfo;
import com.hl.sfzy.dto.ProblemDTO;
import com.hl.sfzy.entity.consutQA.Problem;
import com.hl.sfzy.entity.consutQA.Reply;
import com.hl.sfzy.service.consultQAService.ConsultService;
import com.hl.sfzy.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/consult")
public class ConsultController {


    @Autowired
    private ConsultService consultService;


    @GetMapping("/list/{id}")
    public ProblemDTO list(@PathVariable("id") Long id) {
        ProblemDTO problemAndReply = consultService.getProblemAndReply(id);
        System.err.println(problemAndReply);
        return problemAndReply;
    }


    @GetMapping("/all")
    public R<PageInfo<Problem>> getProblems(Integer pageNum,Integer pageSize){
        PageInfo<Problem> all = consultService.getAll(pageNum,pageSize);
        return Optional.ofNullable(all).map(R::success).orElseGet(() -> R.failure("查询失败"));
    }

    @GetMapping("problem/{id}")
    public R<Problem> getProblemById(@PathVariable("id") String id){
        System.out.println(id);
        Problem problemById = consultService.getProblemById(Long.parseLong(id));
        return Optional.ofNullable(problemById).map(R::success).orElseGet(() -> R.failure("查询失败"));
    }


    @DeleteMapping("/problem/delete/{id}")
    public R<String> removeProblem(@PathVariable("id") Long id){
        int i = consultService.deleteProblem(id);
        return i > 0 ? R.success("删除成功") : R.failure("删除失败");
    }

    @PostMapping("/problem/add")
    public R<String> addProblem(@RequestBody Problem problem){
        int i = consultService.addProblem(problem);
        return i > 0 ? R.success("插入成功") : R.failure("插入失败");
    }

    @PostMapping("/problem/comment")
    public R<String> insertProblemComment(@RequestBody Reply reply){
        int i = consultService.insertProblemComment(reply);
        return i > 0 ? R.success("插入成功") : R.failure("插入失败");

//        System.out.println(reply);
//        return null;
    }


}
